<?php
class FileeditController extends Controller {
	public function index($param) {
		$this->getSmarty ();
		//定义欲读取的目录路径，方便演示，本程序读取的是当前文件所在目录
		$basepath = '.\templates';
		$filterpath;
		$savepath;
		$type = $param['type'];
		$name = $param['name'];
		$path = $param['path'];
		
		$bafilePath;
		$filename;
// 		echo($type."--".$name."---".$path);
		
		if(empty($path)){
			$path = 'templates';
		}
		$path = str_ireplace('.',DS,$path);
		if($type == 0){
			//文件夹点击
			if($name != ""){
				//向下获取文件
				$filterpath = $path.DS.$name;
			}else{
				//向上获取文件
				if($path !="templates" ){
					$filterpath = substr($path,0,strrpos($path,DS));
					//向上截取
				}else {
					$filterpath = $path;
				}
			}
		}else if ($type == 1) {
			$filterpath = $path;
			if($name != ""){
				 $filename = $path.DS.$name;
				 $openfilename = '.'.DS.$filename;
				 $fp=fopen($openfilename,"r");
				 $output=fread($fp,filesize($openfilename));
				 $output = str_ireplace('textarea','$textarea',$output);
				 fclose($fp);
			}
		}
		$tpath = '.'.DS.$filterpath;
// 		echo '<br />';
// 		echo($tpath."--".$path."---".$filterpath);
		//获取文件列表数组
		$folders = self::ReadFolder($tpath,2);
		$files = self::ReadFolder($tpath,1);
		
		//处理文件列表数组
// 		foreach ($files as $value) {
// 			//显示文件链接
// 			echo '<a href="' . $value . '">' . $value . '</a>';
// 			//为方便查看，输出一个 <br /> 换行符
// 			echo '<br />';
// 		}
		$filterpath =  str_ireplace(DS,'.',$filterpath);
		$this->smarty->assign ( 'filecontent',$output);
		$this->smarty->assign ( 'openfilepath',$filename);
		$this->smarty->assign ( 'path',$filterpath);
		$this->smarty->assign ( 'folders',$folders);
		$this->smarty->assign ( 'files',$files);
		$this->smarty->display("admin/file/fileedit.tpl");
	}
	function savefile(){
		$this->getSmarty ();
		$openfilepath = $_POST ["openfilepath"];
		$tpath = '.'.DS.$openfilepath;
		$text = $_POST ["text"];
		$output = str_ireplace('$textarea','textarea',$text);
		$fp=fopen($tpath,"w+");
		fwrite($fp,$output);
		fclose($fp);
		echo  1;
	}
	
	/* 定义自定义函数 */
	/**
	 * 获取文件列表
	 *
	 * @param string  $dir  欲读取的目录路径
	 * @param boolean $mode 0：读取全部；1：仅读取文件；2：仅读取目录
	 * @return array
	 */
	function ReadFolder($dir, $mode = 0) {
		//如果打开目录句柄失败，则输出空数组
		if (!$handle = @opendir($dir)) return array();
		//定义文件列表数组
		$files = array();
		//遍历目录句柄中的条目
		while (false !== ($file = @readdir($handle))) {
			//跳过本目录以及上级目录
			if ('.' === $file || '..' === $file) continue;
			//是否仅读取目录
			if ($mode === 2) {
				if (self::isDir($dir . '/' . $file)) $files[] = $file;
				//是否仅读取文件
			} elseif ($mode === 1) {
				if (self::isFile($dir . '/' . $file)) $files[] = $file;
				//读取全部
			} else {
				$files[] = $file;
			}
		}
		//关闭打开的目录句柄
		@closedir($handle);
		//输出文件列表数组
		return $files;
	}
	
	/**
	 * 判断输入是否为目录
	 *
	 * @param string $dir
	 * @return boolean
	 */
	function isDir($dir) {
		return $dir ? is_dir($dir) : false;
	}
	/**
	 * 判断输入是否为文件
	 *
	 * @param string $file
	 * @return boolean
	 */
	function isFile($file) {
		return $file ? is_file($file) : false;
	}
}
